package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;

public class DeleteNoteController implements Initializable {

    @FXML
    private Button showSchedule;

    @FXML
    private Button searchRommTable;

    @FXML
    private Button SearchTecherTable;

    @FXML
    private Button ClassNote;

    @FXML
    private Button vieNoteBtn;

    @FXML
    private Button newNote;

    @FXML
    private Button deleteNote;

    @FXML
    private ChoiceBox<String> selectCourse;

    @FXML
    private Button seeNote;

    @FXML
    private TableView<Note> NoteTable;

    @FXML
    private TableColumn<Note, String> noteOrder;

    @FXML
    private TableColumn<Note, String> noteTitle;

    @FXML
    private TableColumn<Note, String> noteContent;

    @FXML
    private Button delete;
    @FXML
    private Label hint;
    String xuehao,courseSelected;
    ObservableList<Note> list= FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        hint.setVisible(false);

        noteOrder.setCellValueFactory(new PropertyValueFactory<>("order"));
        noteTitle.setCellValueFactory(new PropertyValueFactory<>("title"));
        noteContent.setCellValueFactory(new PropertyValueFactory<>("content"));
        NoteTable.setItems(list);
        NoteTable.setVisible(false);

        //得到学生学号
        try {
            Scanner input=new Scanner(new File("src/res/info.txt"));
            while (input.hasNext())
            {
                xuehao=input.next();
            }
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        selectCourse.getItems().addAll(MysqlConn.getCourseListOnly(xuehao));

    }


    @FXML
    void deleteNote(ActionEvent event) {
        int selectedID=NoteTable.getSelectionModel().getSelectedIndex();
        NoteTable.getItems().remove(selectedID);
        selectedID+=1;
        String index=Integer.toString(selectedID);
        if(MysqlConn.deleteNote(xuehao,courseSelected,index)==1)  hint.setVisible(true);
        else{
            hint.setText("删除失败！");
            hint.setVisible(true);
        }
    }

    @FXML
    void showNote(ActionEvent event) {

        courseSelected=selectCourse.getValue();
        String[][] note=MysqlConn.getContent(xuehao,courseSelected);
        for(int i=0;i<note.length;i++)
        {
            list.add(new Note(note[i][0],note[i][1],note[i][2]));
        }
        NoteTable.setVisible(true);
    }
    /*
=================================================================以下是界面转换====================================================================
     */

    @FXML
    void showSchedulePage(ActionEvent event) {
        SceneManager.SwitchToSchedulePage(event);
    }

    @FXML
    void SwitchToDeletePage(ActionEvent event) {
        SceneManager.SwitchToDeleteNotePage(event);
    }

    @FXML
    void SwitchToNewNotePage(ActionEvent event) {
        SceneManager.SwitchToAddNotePage(event);
    }

    @FXML
    void SwitchToNotePage(ActionEvent event) {
        SceneManager.SwitchToNotePage(event);
    }

    @FXML
    void SwitchToSearchRoomTtblePage(ActionEvent event) {
        SceneManager.SwitchToSearchRoomTtblePage(event);
    }

    @FXML
    void SwitchToSearchTeacherTtblePage(ActionEvent event) {
        SceneManager.SwitchToSearchTeacherTtblePage(event);
    }

    @FXML
    void SwitchtoVIewNotePage(ActionEvent event) {
        SceneManager.SwitchToNotePage(event);
    }


}
